/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.persistencia.controller;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import mrp.persistencia.entity.Tallatacosuela;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.transaction.UserTransaction;
import mrp.beans.util.EntityManagerUtil;
import mrp.persistencia.controller.exceptions.IllegalOrphanException;
import mrp.persistencia.controller.exceptions.NonexistentEntityException;
import mrp.persistencia.controller.exceptions.PreexistingEntityException;
import mrp.persistencia.controller.exceptions.RollbackFailureException;
import mrp.persistencia.entity.Detalletalla;
import mrp.persistencia.entity.Talla;

/**
 *
 * @author root
 */
public class TallaJpaController implements Serializable {

    public TallaJpaController() {
        
        this.emf = EntityManagerUtil.getEntityManagerFactory();
    }

    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Talla talla) throws PreexistingEntityException, RollbackFailureException, Exception {
        if (talla.getTallatacosuelaList() == null) {
            talla.setTallatacosuelaList(new ArrayList<Tallatacosuela>());
        }
        if (talla.getDetalletallaList() == null) {
            talla.setDetalletallaList(new ArrayList<Detalletalla>());
        }
        EntityManager em = null;
        try {
            
            em = getEntityManager();
            em.getTransaction().begin();
            List<Tallatacosuela> attachedTallatacosuelaList = new ArrayList<Tallatacosuela>();
            for (Tallatacosuela tallatacosuelaListTallatacosuelaToAttach : talla.getTallatacosuelaList()) {
                tallatacosuelaListTallatacosuelaToAttach = em.getReference(tallatacosuelaListTallatacosuelaToAttach.getClass(), tallatacosuelaListTallatacosuelaToAttach.getCodtallatacosuela());
                attachedTallatacosuelaList.add(tallatacosuelaListTallatacosuelaToAttach);
            }
            talla.setTallatacosuelaList(attachedTallatacosuelaList);
            List<Detalletalla> attachedDetalletallaList = new ArrayList<Detalletalla>();
            for (Detalletalla detalletallaListDetalletallaToAttach : talla.getDetalletallaList()) {
                detalletallaListDetalletallaToAttach = em.getReference(detalletallaListDetalletallaToAttach.getClass(), detalletallaListDetalletallaToAttach.getCoddetalletalla());
                attachedDetalletallaList.add(detalletallaListDetalletallaToAttach);
            }
            talla.setDetalletallaList(attachedDetalletallaList);
            em.persist(talla);
            for (Tallatacosuela tallatacosuelaListTallatacosuela : talla.getTallatacosuelaList()) {
                Talla oldTallaFkOfTallatacosuelaListTallatacosuela = tallatacosuelaListTallatacosuela.getTallaFk();
                tallatacosuelaListTallatacosuela.setTallaFk(talla);
                tallatacosuelaListTallatacosuela = em.merge(tallatacosuelaListTallatacosuela);
                if (oldTallaFkOfTallatacosuelaListTallatacosuela != null) {
                    oldTallaFkOfTallatacosuelaListTallatacosuela.getTallatacosuelaList().remove(tallatacosuelaListTallatacosuela);
                    oldTallaFkOfTallatacosuelaListTallatacosuela = em.merge(oldTallaFkOfTallatacosuelaListTallatacosuela);
                }
            }
            for (Detalletalla detalletallaListDetalletalla : talla.getDetalletallaList()) {
                Talla oldTallaFkOfDetalletallaListDetalletalla = detalletallaListDetalletalla.getTallaFk();
                detalletallaListDetalletalla.setTallaFk(talla);
                detalletallaListDetalletalla = em.merge(detalletallaListDetalletalla);
                if (oldTallaFkOfDetalletallaListDetalletalla != null) {
                    oldTallaFkOfDetalletallaListDetalletalla.getDetalletallaList().remove(detalletallaListDetalletalla);
                    oldTallaFkOfDetalletallaListDetalletalla = em.merge(oldTallaFkOfDetalletallaListDetalletalla);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            try {
                em.getTransaction().rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findTalla(talla.getCodtalla()) != null) {
                throw new PreexistingEntityException("Talla " + talla + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Talla talla) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            
            em = getEntityManager();
            em.getTransaction().begin();
            Talla persistentTalla = em.find(Talla.class, talla.getCodtalla());
            List<Tallatacosuela> tallatacosuelaListOld = persistentTalla.getTallatacosuelaList();
            List<Tallatacosuela> tallatacosuelaListNew = talla.getTallatacosuelaList();
            List<Detalletalla> detalletallaListOld = persistentTalla.getDetalletallaList();
            List<Detalletalla> detalletallaListNew = talla.getDetalletallaList();
            List<String> illegalOrphanMessages = null;
            for (Tallatacosuela tallatacosuelaListOldTallatacosuela : tallatacosuelaListOld) {
                if (!tallatacosuelaListNew.contains(tallatacosuelaListOldTallatacosuela)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Tallatacosuela " + tallatacosuelaListOldTallatacosuela + " since its tallaFk field is not nullable.");
                }
            }
            for (Detalletalla detalletallaListOldDetalletalla : detalletallaListOld) {
                if (!detalletallaListNew.contains(detalletallaListOldDetalletalla)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Detalletalla " + detalletallaListOldDetalletalla + " since its tallaFk field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Tallatacosuela> attachedTallatacosuelaListNew = new ArrayList<Tallatacosuela>();
            for (Tallatacosuela tallatacosuelaListNewTallatacosuelaToAttach : tallatacosuelaListNew) {
                tallatacosuelaListNewTallatacosuelaToAttach = em.getReference(tallatacosuelaListNewTallatacosuelaToAttach.getClass(), tallatacosuelaListNewTallatacosuelaToAttach.getCodtallatacosuela());
                attachedTallatacosuelaListNew.add(tallatacosuelaListNewTallatacosuelaToAttach);
            }
            tallatacosuelaListNew = attachedTallatacosuelaListNew;
            talla.setTallatacosuelaList(tallatacosuelaListNew);
            List<Detalletalla> attachedDetalletallaListNew = new ArrayList<Detalletalla>();
            for (Detalletalla detalletallaListNewDetalletallaToAttach : detalletallaListNew) {
                detalletallaListNewDetalletallaToAttach = em.getReference(detalletallaListNewDetalletallaToAttach.getClass(), detalletallaListNewDetalletallaToAttach.getCoddetalletalla());
                attachedDetalletallaListNew.add(detalletallaListNewDetalletallaToAttach);
            }
            detalletallaListNew = attachedDetalletallaListNew;
            talla.setDetalletallaList(detalletallaListNew);
            talla = em.merge(talla);
            for (Tallatacosuela tallatacosuelaListNewTallatacosuela : tallatacosuelaListNew) {
                if (!tallatacosuelaListOld.contains(tallatacosuelaListNewTallatacosuela)) {
                    Talla oldTallaFkOfTallatacosuelaListNewTallatacosuela = tallatacosuelaListNewTallatacosuela.getTallaFk();
                    tallatacosuelaListNewTallatacosuela.setTallaFk(talla);
                    tallatacosuelaListNewTallatacosuela = em.merge(tallatacosuelaListNewTallatacosuela);
                    if (oldTallaFkOfTallatacosuelaListNewTallatacosuela != null && !oldTallaFkOfTallatacosuelaListNewTallatacosuela.equals(talla)) {
                        oldTallaFkOfTallatacosuelaListNewTallatacosuela.getTallatacosuelaList().remove(tallatacosuelaListNewTallatacosuela);
                        oldTallaFkOfTallatacosuelaListNewTallatacosuela = em.merge(oldTallaFkOfTallatacosuelaListNewTallatacosuela);
                    }
                }
            }
            for (Detalletalla detalletallaListNewDetalletalla : detalletallaListNew) {
                if (!detalletallaListOld.contains(detalletallaListNewDetalletalla)) {
                    Talla oldTallaFkOfDetalletallaListNewDetalletalla = detalletallaListNewDetalletalla.getTallaFk();
                    detalletallaListNewDetalletalla.setTallaFk(talla);
                    detalletallaListNewDetalletalla = em.merge(detalletallaListNewDetalletalla);
                    if (oldTallaFkOfDetalletallaListNewDetalletalla != null && !oldTallaFkOfDetalletallaListNewDetalletalla.equals(talla)) {
                        oldTallaFkOfDetalletallaListNewDetalletalla.getDetalletallaList().remove(detalletallaListNewDetalletalla);
                        oldTallaFkOfDetalletallaListNewDetalletalla = em.merge(oldTallaFkOfDetalletallaListNewDetalletalla);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            try {
                em.getTransaction().rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = talla.getCodtalla();
                if (findTalla(id) == null) {
                    throw new NonexistentEntityException("The talla with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws IllegalOrphanException, NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
            
            em = getEntityManager();
            em.getTransaction().begin();
            Talla talla;
            try {
                talla = em.getReference(Talla.class, id);
                talla.getCodtalla();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The talla with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Tallatacosuela> tallatacosuelaListOrphanCheck = talla.getTallatacosuelaList();
            for (Tallatacosuela tallatacosuelaListOrphanCheckTallatacosuela : tallatacosuelaListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Talla (" + talla + ") cannot be destroyed since the Tallatacosuela " + tallatacosuelaListOrphanCheckTallatacosuela + " in its tallatacosuelaList field has a non-nullable tallaFk field.");
            }
            List<Detalletalla> detalletallaListOrphanCheck = talla.getDetalletallaList();
            for (Detalletalla detalletallaListOrphanCheckDetalletalla : detalletallaListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Talla (" + talla + ") cannot be destroyed since the Detalletalla " + detalletallaListOrphanCheckDetalletalla + " in its detalletallaList field has a non-nullable tallaFk field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(talla);
            em.getTransaction().commit();
        } catch (Exception ex) {
            try {
                em.getTransaction().rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Talla> findTallaEntities() {
        return findTallaEntities(true, -1, -1);
    }

    public List<Talla> findTallaEntities(int maxResults, int firstResult) {
        return findTallaEntities(false, maxResults, firstResult);
    }

    private List<Talla> findTallaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Talla.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Talla findTalla(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Talla.class, id);
        } finally {
            em.close();
        }
    }

    public int getTallaCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Talla> rt = cq.from(Talla.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    public List<Talla> findTallaWhereOrderBy(String consulta,boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
//            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
//            cq.select(cq.from(Cliente.class));
//            Query q = em.createQuery(cq);
//            Query q = em.createQuery("SELECT o FROM Cliente o where "+ +" order by o.numerocliente");
            Query q = em.createQuery(consulta);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
}
